package java_thinking.unit_10._10_7.Exercise;

/**
 * @Description :
 * @author: cww
 * @DateTime: 2019-03-12 21:50
 */
interface U_E23 {
    void method1();
    void method2();
    void method3();
}


class A{
    public U_E23 methodA(){
        return  new U_E23() {
            @Override
            public void method1() {
                System.out.println("A.methodA().method1()");
            }

            @Override
            public void method2() {
                System.out.println("A.methodA().method2()");

            }

            @Override
            public void method3() {
                System.out.println("A.methodA().method3()");

            }
        };
    }
}

class B{
    public U_E23[] u_e23s;
    public B(int i){
        u_e23s = new U_E23[i];
    }

    /**
     * 可以接受对U的引用并存储到数组中
     * @param elem
     * @return
     */
    public boolean add(U_E23 elem){
        for (int i =0;i<u_e23s.length;i++){
            if (u_e23s == null){
                u_e23s[i] =elem;
                return true;
            }
        }
        return false;
    }

    /**
     * 将数组的引用设为null
     * @param i
     * @return
     */
    public boolean setNull(int i){
        if (i<0||i>=u_e23s.length){
            return false;
        }else {
            u_e23s[i] = null;
        }
        return true;
    }

    /**
     * 遍历接口数组，并调用接口中的方法
     */
    public void callMethod(){
        for (int i =0;i<u_e23s.length;i++){
            if (u_e23s[i]!=null){
                u_e23s[i].method1();
                u_e23s[i].method2();
                u_e23s[i].method3();
            }
        }
    }
}

class E23_UAB{
    public static void main(String[] args) {
        A[] a = {new A(),new A(),new A()};
        B b = new B(3);
        for (int i=0;i<a.length;i++){
            b.add(a[i].methodA());
            b.callMethod();
            System.out.println("***********");
            b.setNull(0);
            b.callMethod();
        }

    }
}